//
//  MYXMyInformationController.m
//  meiyinxiang
//
//  Created by lizidi on 2018/1/11.
//  Copyright © 2018年 hbjApple. All rights reserved.
//

#import "MYXMyInformationController.h"
#import "MYY_walletNavView.h"
#import "InformationHeadView.h"
#import "InformationTableOneCell.h"
#import "InformationTableTwoCell.h"
#import "MYCIndexCardCell.h"
#import "MYYChangeInforViewController.h"
#import "MYXotheruserinfoModel.h"
#import "UIImageView+LBBlurredImage.h"
#import "MYXInformationViewModel.h"
#import "MYXotherinfoListModel.h"
#import "InformationThreeCell.h"

static NSString *const MYXtableOneCellIdentifier = @"tableOneCell";
static NSString *const MYXtableTwoCellIdentifier = @"tableTwoCell";
static NSString *const MYXtableThreeCellIdentifier = @"tableThreeCell";

static NSString *const MYCIndexCardCellIdentifier = @"MYCIndexCardCell";

@interface MYXMyInformationController ()<UITableViewDelegate,UITableViewDataSource>
{
    InformationTableOneCell * tableOneCell;
    InformationTableTwoCell * tableTwoCell;
    InformationThreeCell * tableThreeCell;
    
    MYCIndexCardCell *MYCIndexCardCe ;
    MYXInformationViewModel * modle;
    //    MYXotheruserinfoModel * model;
    
    
}
@property(nonatomic,strong)MYY_walletNavView * inforMationNav;
@property(nonatomic,strong)UIView * headtableView;
@property(nonatomic,strong)UIView * headView;
@property(nonatomic,strong)InformationHeadView * informationHeadView;
@property(nonatomic,strong)UITableView * myTableView;
@property(nonatomic,strong) UIImageView *headerView;
@property (nonatomic, strong)MYXotheruserinfoModel *MYXotheruserinfoM;
@property (nonatomic,strong)MYXotherinfoListModel * infoListModle;

@end

@implementation MYXMyInformationController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    [self initIndexCardRequest:self.userID];
    
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if (modle ==nil) {
        modle = [[MYXInformationViewModel alloc]init];
    }
    if (self.infoListModle==nil) {
        self.infoListModle = [[MYXotherinfoListModel alloc]init];
    }
    [self viewDidLayoutSubviews];
    [self createView];
    
}

-(UITableView *)myTableView{
    if (!_myTableView) {
        _myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, -[MyAdapter laDapter:24], ScreenWidth, ScreenHeight+[MyAdapter laDapter:24]) style:UITableViewStyleGrouped];
        _myTableView.backgroundColor = RGBACOLOR(242, 242, 242, 1);
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        [_myTableView registerClass:[tableOneCell class] forCellReuseIdentifier:MYXtableOneCellIdentifier];
        [_myTableView registerClass:[tableTwoCell class] forCellReuseIdentifier:MYXtableTwoCellIdentifier];
        [_myTableView registerClass:[tableThreeCell class] forCellReuseIdentifier:MYXtableThreeCellIdentifier];
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_myTableView registerNib:[UINib nibWithNibName:MYCIndexCardCellIdentifier bundle:nil] forCellReuseIdentifier:MYCIndexCardCellIdentifier];
        _myTableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, [MyAdapter laDapter:220])];
        _myTableView.sectionHeaderHeight = [MyAdapter laDapter:10];
        _myTableView.sectionFooterHeight = [MyAdapter laDapter:5];
        _myTableView.showsVerticalScrollIndicator = NO;
        [_myTableView addSubview:self.headerView];
    }
    return _myTableView;
}
- (UIImageView *)headerView{
    if (!_headerView) {
        _headerView = [[UIImageView alloc] initWithFrame:CGRectMake(0,-[MyAdapter laDapter:24], ScreenWidth, [MyAdapter laDapter:220])];
        [_headerView setContentMode:UIViewContentModeScaleAspectFill];
        [_headerView setClipsToBounds:YES];
    }
    return _headerView;
}

-(UIView*)headView{
    if (!_headView) {
        _headView = [[UIView alloc]initWithFrame:CGRectMake(0, -[MyAdapter laDapter:24], ScreenWidth, [MyAdapter laDapter:220])];
    }
    
    return _headView;
}

-(InformationHeadView *)informationHeadView{
    if(!_informationHeadView){
        _informationHeadView = [[InformationHeadView alloc]initWithFrame:CGRectMake(0, 0,ScreenWidth, [MyAdapter laDapter:220])];
    }
    return _informationHeadView;
}


#pragma mark - Navigation
-(void)initNavigation{
    
    WS(weakSelf);
    _inforMationNav = [[MYY_walletNavView alloc]init];
    _inforMationNav.nameLable.text = @"";
    [self.view addSubview:_inforMationNav];
    [self.inforMationNav mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.height.equalTo(@(SafeAreaTopHeight));
    }];
    self.inforMationNav.backButton =^{
        [weakSelf.navigationController popViewControllerAnimated:YES];
    };
    
    if ([[MYXMemberMdel sharedInstance].userinfo.userid isEqualToString:self.MYXotheruserinfoM.userID]) {
        [self.inforMationNav.rightBtn setImage:[UIImage imageNamed:@"ddd"] forState:UIControlStateNormal];
        self.inforMationNav.rightButton =^{
            MYYChangeInforViewController * vc = [[MYYChangeInforViewController alloc]init];
            vc.navigationItem.title = @"个人资料";
            [weakSelf.navigationController pushViewController:vc animated:YES];
        };
    }else{
        [self.inforMationNav.rightBtn setUserInteractionEnabled:NO];
    }
    
}

#pragma mark -CreateView
-(void)createView{
    
    [self.view addSubview:self.myTableView];
    [self initNavigation];
    [self addChildViews];
    //    [self.headerView sd_setImageWithURL:[NSURL URLWithString:self.MYXotheruserinfoM.image]];
    
}
-(void)addChildViews{
    [self.myTableView setTableHeaderView:self.headView];
    [self.headerView setFrame:self.headView.bounds];
    [self.headView addSubview:self.headerView];
    [self.headView addSubview:self.informationHeadView];
}



#pragma mark --TableView delegate

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0 ) {
        return 1;
    }else{
        return 3;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return [MyAdapter laDapter:94];
    }else{
        return [MyAdapter laDapter:74];
    }
    
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        
        if (self.infoListModle.file.length>0) {
            tableOneCell = [[InformationTableOneCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:MYXtableOneCellIdentifier];
            tableOneCell.selectionStyle = UITableViewCellSelectionStyleNone;
            tableOneCell.MYXotherinfoListModel = self.infoListModle;
            return tableOneCell;
        }else{
            tableTwoCell = [[InformationTableTwoCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:MYXtableTwoCellIdentifier];
            tableTwoCell.selectionStyle = UITableViewCellSelectionStyleNone;
            return tableTwoCell;
        }
    }else if(indexPath.section==1){
        if (indexPath.row==0||indexPath.row==1) {
            MYCIndexCardCe = [tableView dequeueReusableCellWithIdentifier:MYCIndexCardCellIdentifier forIndexPath:indexPath];
            MYCIndexCardCe.selectionStyle = UITableViewCellSelectionStyleNone;
            if (indexPath.row==0) {
                MYCIndexCardCe.homeLabel.text =@"家乡";
                MYCIndexCardCe.homeLabel.font = [MyAdapter lfontADapter:15];
                MYCIndexCardCe.homeLabel.textColor = RGBACOLOR(29, 29, 29, 1);
                MYCIndexCardCe.titileLabel.text = self.MYXotheruserinfoM.home;
            }else if (indexPath.row==1){
                MYCIndexCardCe.homeLabel.text =@"生日";
                MYCIndexCardCe.homeLabel.font = [MyAdapter lfontADapter:15];
                MYCIndexCardCe.homeLabel.textColor = RGBACOLOR(29, 29, 29, 1);
                if ([self.MYXotheruserinfoM.birthday isEqualToString:@"0,"]) {
                    MYCIndexCardCe.titileLabel.text = @"";
                }else{
                    MYCIndexCardCe.titileLabel.text =  [MYXDateTimeHelper birthDayTimeWithIntervalString:self.MYXotheruserinfoM.birthday];
                }
                
            }
            return MYCIndexCardCe;
        }else{
            tableThreeCell = [[InformationThreeCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:MYXtableThreeCellIdentifier];
            tableThreeCell.selectionStyle = UITableViewCellSelectionStyleNone;
            tableThreeCell.MYXotheruserinfoM = self.MYXotheruserinfoM;
            
            return tableThreeCell;
        }
        
    }else{
        return nil;
    }
    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, [MyAdapter laDapter:10])];
    view.backgroundColor = RGBACOLOR(242, 242, 242, 1);
    return view;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat imageHeight = self.headView.frame.size.height;
    CGFloat imageWidth  = ScreenWidth;
    CGFloat imageOffsetY = scrollView.contentOffset.y;
    if (imageOffsetY < 0) {
        CGFloat totalOffset = imageHeight + ABS(imageOffsetY);
        CGFloat f = totalOffset / imageHeight;
        
        self.headerView.frame = CGRectMake(-(imageWidth * f - imageWidth) * 0.5, imageOffsetY, imageWidth * f, totalOffset);
    }
    
}
-(void)initIndexCardRequest:(NSString *)userID{
    [SVProgressHUD show];
    self.MYXotheruserinfoM =[[MYXotheruserinfoModel alloc]init];
    //    [modle loadMyInformationModelView:userID WithResult:^(BOOL isSuccess,NSString *message) {
    //        if (isSuccess) {
    //            self.informationHeadView.otheruserinfoModel = self.MYXotheruserinfoM;
    //            [self.myTableView reloadData];
    //            [self.headerView sd_setImageWithURL:[NSURL URLWithString:self.MYXotheruserinfoM.image]placeholderImage:[UIImage imageNamed:@"AppIcon"]];
    //        }else{
    //
    //        }}];
    [[MYXHttpManager shared]otheruserinfo:userID withResult:^(BOOL success, NSString *msg, id object) {
        if (success) {
            if (object) {
                [SVProgressHUD dismiss];
                self.MYXotheruserinfoM= object;
                self.informationHeadView.otheruserinfoModel = self.MYXotheruserinfoM;
                [self.myTableView reloadData];
//                [self.headerView sd_setImageWithURL:[NSURL URLWithString:self.MYXotheruserinfoM.image]placeholderImage:[UIImage imageNamed:@"AppIcon"]];
                [self.headerView sd_setImageWithURL:[NSURL URLWithString:[MYXMemberMdel sharedInstance].userinfo.image] placeholderImage:[UIImage imageNamed:@"AppIcon"]];

            }
        }else{
        
        }
       
    } errorResult:^(NSError *enginerError) {
        [SVProgressHUD dismiss];
        // [JXTHUDHelper showHUDText:enginerError type:@"error" inView:nil];
    }];
    
    [[MYXHttpManager shared]otheruserinfoList:userID withResult:^(BOOL success, NSString *msg, id object) {
        if (object) {
            
            self.infoListModle = object;
            [self.myTableView reloadData];
            
        }
        
    } errorResult:^(NSError *enginerError){
        
        
    }];
    
    //    [[MYXHttpManager share d]otheruserinfo:userID addBlock:^(MYXotheruserinfoModel *detailModel, NSError *error) {
    //        if (!error) {
    //            [SVProgressHUD dismiss];
    //            self.MYXotheruserinfoM = detailModel;
    //            self.informationHeadView.otheruserinfoModel =self.MYXotheruserinfoM;
    //            [self.myTableView reloadData];
    //            [self.headerView sd_setImageWithURL:[NSURL URLWithString:self.MYXotheruserinfoM.image]placeholderImage:[UIImage imageNamed:@"AppIcon"]];
    //        }
    //}];
    
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

